﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using Autofac.Integration.Mvc;
using System.Reflection;
using System.Web.Mvc;
using Autofac.Integration.WebApi;
using System.Web.Http;

namespace AutoFacPack
{
    public static class AutoFacEngine
    {
        public static IContainer _container;
        private static ContainerManager _containerManager;
        #region MVC项目
        /// <summary>
        /// 注册依赖注入
        /// </summary>
        private static void RegisterDependencies(List<AutoFacRegisterModel> autoFacRegisterModels, HttpConfiguration config = null)
        {
            var builder = new ContainerBuilder();

            for (int i = 0; autoFacRegisterModels != null && i < autoFacRegisterModels.Count; i++)
            {
                switch (autoFacRegisterModels[i].RegisterType)
                {
                    case AutoFacPack.RegisterType.Controller:
                        builder.RegisterControllers(autoFacRegisterModels[i].Assembly);
                        builder.RegisterControllers(autoFacRegisterModels[i].Assembly).PropertiesAutowired(new PropertySelector());
                        break;
                    case AutoFacPack.RegisterType.ApiController:
                        builder.RegisterApiControllers(autoFacRegisterModels[i].Assembly);
                        builder.RegisterApiControllers(autoFacRegisterModels[i].Assembly).PropertiesAutowired(new PropertySelector());
                        break;
                    case AutoFacPack.RegisterType.IsService:
                        Type type = autoFacRegisterModels[i].Type;
                        var ret = builder.RegisterAssemblyTypes(autoFacRegisterModels[i].Assembly).Where(t => type.IsAssignableFrom(t)).PropertiesAutowired(new PropertySelector(type));
                        break;
                    default:
                        break;
                }
            }
            _container = builder.Build();

            if (config != null)
            {
                //注册api容器需要使用HttpConfiguration对象
                config.DependencyResolver = new AutofacWebApiDependencyResolver(_container);
            }
            DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
            _containerManager = new ContainerManager(_container);

        }
        /// <summary>
        /// 初始化.
        /// </summary>
        /// <param name="controllerAssemblyName">controller程序集名称</param>
        public static void Initialize(List<AutoFacRegisterModel> autoFacRegisterModels, HttpConfiguration config)
        {
            //注册依赖注入
            RegisterDependencies(autoFacRegisterModels, config);
        }
        /// <summary>
        /// 初始化.
        /// </summary>
        /// <param name="controllerAssemblyName">controller程序集名称</param>
        public static void Initialize(List<AutoFacRegisterModel> autoFacRegisterModels)
        {
            //注册依赖注入
            RegisterDependencies(autoFacRegisterModels, null);
        }
        #endregion
        /// <summary>
        /// 解析方法
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <returns></returns>
        public static T Resolve<T>(String Key="") where T : class
        {
            return ContainerManager.Resolve<T>(Key);
        }
        /// <summary>
        /// 解析方法
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <returns></returns>
        public static T ResolveNamed<T>(String name = "") where T : class
        {
            return ContainerManager.ResolveNamed<T>(name);
        }
        /// <summary>
        ///  解析方法
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static object Resolve(Type type)
        {
            return ContainerManager.Resolve(type);
        }

        /// <summary>
        /// 批量解析方法
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <returns></returns>
        public static T[] ResolveAll<T>()
        {
            return ContainerManager.ResolveAll<T>();
        }
        /// <summary>
        /// 缓存管理器
        /// </summary>
        public static ContainerManager ContainerManager
        {
            get { return _containerManager; }
        }
    }
}